package com.jwds.digital.bios.business.service.anomaly;

import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;

import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 异常验证工厂
 * @author hesong
 */
@Component
public class AnomalyVerifyFactory implements InitializingBean {

    private static final Map<String, AnomalyVerifyInterface> VERIFY_INTERFACE_MAP = new ConcurrentHashMap<>();
    @Autowired
    private ApplicationContext context;

    @Override
    public void afterPropertiesSet() throws Exception {
        Map<String, AnomalyVerifyInterface> beansMap = context.getBeansOfType(AnomalyVerifyInterface.class);
        for (AnomalyVerifyInterface value : beansMap.values()) {
            VERIFY_INTERFACE_MAP.put(value.getDeductionItem(), value);
        }
    }

    public AnomalyVerifyInterface getHandlerInstance(String deductionItem) {
        return VERIFY_INTERFACE_MAP.get(deductionItem);
    }

    public Set<String> getRuleCodes() {
        return VERIFY_INTERFACE_MAP.keySet();
    }
}
